"use strict";
const path = require("path");
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin')

function resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = {
  publicPath: process.env.BASE_URL,
  productionSourceMap: false,
  devServer: {
    port: 3000,
    proxy: {
      // 监控
      '/actuator': {
        target: 'http://10.0.11.185:38403'
      },
      // 所有组件
      '/api': {
        target: 'http://10.0.11.185:38403'
      },
      '/ureport': {
        target: 'http://10.0.11.185:38403',
      },
      '/magic': {
        target: 'http://10.0.11.185:38403',
      }
    },
    headers: {
      // 微前端子应用，允许跨域
      'Access-Control-Allow-Origin': '*',
    },
  },
  chainWebpack: config => {
    config.when(process.env.NODE_ENV !== "development", config => {
      // 移除 prefetch 插件
      config.plugins.delete("prefetch");

      config.optimization.splitChunks({
        chunks: "all",
        cacheGroups: {
          tinymce: {
            name: "chunk-tinymce",
            test: /[\\/]tinymce[\\/]/,
            priority: 13
          },
          ychbpmdesigner: {
            name: "chunk-ych-bpm-designer",
            test: /[\\/]ych-bpm-designer[\\/]/,
            priority: 13
          },
          codemirror: {
            name: "chunk-codemirror",
            test: /[\\/]codemirror[\\/]/,
            priority: 13
          },
          vcolorpicker: {
            name: "chunk-vcolorpicker",
            test: /vcolorpicker(.*)/,
            priority: 13
          },
          libs: {
            name: "chunk-libs",
            test: /[\\/]node_modules[\\/]/,
            priority: 12,
            minChunks: 3,
            chunks: "all"
          },
          vender: {
            name: "chunk-vender",
            test: /[\\/]node_modules[\\/]/,
            priority: 11,
            chunks: "initial"
          },
          commons: {
            name: "chunk-commons",
            test: resolve("src/components"), // can customize your rules
            minChunks: 2, //  minimum common number
            priority: 2,
            reuseExistingChunk: true
          }
        }
      });
    });
  },
  configureWebpack: config => {
    if (process.env.NODE_ENV === "production") {
      config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true;
    }
    return {
      resolve: {
        alias: {
          "@": resolve("src")
        },
      },
      plugins: [
        new MonacoWebpackPlugin()
      ]
      // plugins: [
      //   new BundleAnalyzerPlugin({
      //     analyzerMode: 'server',
      //     generateStatsFile: true,
      //     statsOptions: { source: false }
      //   })
      // ]
    };
  },
  css: {
    loaderOptions: {
      less: {
        modifyVars: {
          /* less 变量覆盖，用于自定义 ant design 主题 */
          "primary-color": "#2F54EB",
          "link-color": "#2F54EB",
          "border-radius-base": "2px"
        },
        javascriptEnabled: true
      }
    }
  }
};
